/*
 * backoff.hpp
 *
 *  Created on: Jul 26, 2010
 *      Author: sdaniele
 */

#ifndef BACKOFF_HPP_
#define BACKOFF_HPP_

#include <boost/mpl/less_equal.hpp>

namespace syscpar{namespace test{


/*
 *
 * Backoff functions define how 'arg' - re-spawning branch factor behaves.
 * Finally, the 'arg' must drop down to 0.
 *
 *
 * Few simple backoff functions defined below.
 *
 */


//one generation of children, no grandsons
template <class>
struct once
	: mpl::identity< mpl::int_<0> >
{};

//decrements branch factor each iteration
template <class I>
struct decr
	: mpl::identity< typename mpl::prior<I>::type >
{};

template <>
struct decr< mpl::int_<0> >
	: mpl::identity< mpl::int_<0> >
{};



//divides by d
template <class I,class d>
struct div_by
	: mpl::divides<I, d>
{};

//halves
template <class I>
struct half
	: div_by<I,mpl::int_<2> >
{};


//substracts s
template <class I, class s>
struct sub
	: mpl::eval_if<
		  mpl::less_equal<mpl::minus<I,s>,mpl::int_<0> >
		, mpl::int_<0>
		, mpl::minus<I,s>
	>
{};


}}

#endif /* BACKOFF_HPP_ */
